package Demo06.Demo07;

import java.io.*;
import java.util.*;

public class Demo06_HashMap {
    public static void main(String[] args) throws IOException {
        HashMap<String, Integer> map = new HashMap<>();
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("G:\\学习\\txt.txt")));
        String lines =null;
        while ((lines = br.readLine())!=null){
            String user =lines;
            String[] split = user.split(",");
            if (map.containsKey(split[1])){
                map.put(split[1],map.get(split[1])+1);
            }else {
                map.put(split[1],1);
            }
        }
        Set<String> set = new HashSet<>();
        for (String key:map.keySet()){
            set.add(key);
        }
        System.out.println("所有的用户"+set);
        HashMap<String, Integer> hashMap = new HashMap<>();
        for (Map.Entry<String,Integer> entry: map.entrySet()){
            if (entry.getValue()>1){
                hashMap.put(entry.getKey(),entry.getValue());
            }
        }
        System.out.println("重复的map："+hashMap);

        Comparator<Map.Entry<String,Integer>> value = new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o1.getValue()-o2.getValue();
            }
        };
        List<Map.Entry<String,Integer>> list =new ArrayList<Map.Entry<String,Integer>>(hashMap.entrySet());
        Collections.sort(list,value);
        System.out.println("排序开始");
        for (Map.Entry<String,Integer> entry :list){
            System.out.println(entry.getKey()+":"+entry.getValue());
        }
        System.out.println("排序结束");
    }
}
